const {app, BrowserWindow, Menu} = require('electron');
const path = require('path');
const template = require('./template')

//Create native application menus and context menus.
//https://www.electronjs.org/docs/latest/api/menu

app.on('ready', () => {
  let win = new BrowserWindow({
    //https://www.electronjs.org/docs/latest/api/browser-window#new-browserwindowoptions
    width: 802,
    height: 550,

    webPreferences: {
      nodeIntegration: true,
      // contextIsolation: true,
      // enableRemoteModule: false,
      // devTools: true
    }
  });

  //template (MenuItemConstructorOptions | MenuItem)[]
  const menu = Menu.buildFromTemplate(template)

  /*
    + `window` BrowserWindow (optional) - Default is the focused window.
    + `x` Number (optional) - Default is the current mouse cursor position. Must be declared if y is declared.
    + `y` Number (optional) - Default is the current mouse cursor position. Must be declared if x is declared.
    + `positioningItem` Number (optional) macOS - The index of the menu item to be positioned under the mouse cursor at the specified coordinates. Default is -1.
    + `callback` Function (optional) - Called when menu is closed.
  */
  menu.popup({
    //Default is the focused window
    // window: win,

    //这个位置是相对于你弹出窗口的左上角的相对位置 (非整个电脑屏幕的左上角
    x:100,
    y:100,
    positioningItem: 3,
    callback(){
      console.log('菜单关闭了')
    }
  });
  // 不像 Menu.setApplicationMenu(menu);
  // 这样使用就相当于是contextmenu
  // 具体生出的样子查看 README.md

  setTimeout(() => {
    console.log('setTimeout到了--- --- ---');
    //
    menu.closePopup(/*window, 默认是 focused window*/);
  },2000);


  win.loadFile(path.resolve(__dirname, 'index.html'))
});

